package com.vanke.agent.sync;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;


import org.apache.log4j.Logger;

import com.vanke.agent.ADataSource;
import com.vanke.agent.C;
import com.vanke.agent.common.CstUtil;

/**
 * 置业顾问身份中间表
 * @author ysm
 * @date 2014-10-20 下午04:34:43
 */
public class MyStationUserSync extends AgentSync {
	Logger log = Logger.getLogger(MyStationUserSync.class);
	String table = "myStationUser";
	String fields = C.FIELDS_MYSTATIONUSER;
	@Override
	public void sync() {
		log.info("=============sync myStationUser===========");
		Connection con = ADataSource.getConnection(ADataSource.SCHEMA_MY);
		Connection wcon = ADataSource.getConnection(ADataSource.SCHEMA_BIZ);
		try {
			String querySql = "SELECT " + fields + "  FROM " + table;
			log.info("select sql: " + querySql);
			PreparedStatement pstmt = con.prepareStatement(querySql);
			ResultSet rs = pstmt.executeQuery();
			int count = 0;
			if (!rs.next()) {
				log.info("no myStationUser data needs to be synchronized");
				ADataSource.close(rs);
				ADataSource.close(pstmt);
				return;
			}
			
			String insertSql = CstUtil.formatInsertSql(table, fields);
			log.info("insert sql: " + insertSql);
			PreparedStatement wpstmt = wcon.prepareStatement(insertSql);
			int columns = fields.split(",").length;
			
			String updateSql = CstUtil.formatUpdateSql(table, fields);
			updateSql += " where StationUserGUID =?";
			log.info("update sql: " + updateSql);
			PreparedStatement dataUpdatePs = wcon.prepareStatement(updateSql);
			
			PreparedStatement dataps = wcon.prepareStatement(querySql);
			ResultSet datars = dataps.executeQuery();
			Map<String,Boolean> dataGuidMap = new HashMap<String,Boolean>();
			while(datars.next()){
				dataGuidMap.put(datars.getString("StationUserGUID").trim(), true);
			}
			
			Map<String,Boolean> myErpGuidMap = new HashMap<String,Boolean>();
			
			do{
				myErpGuidMap.put(rs.getString("StationUserGUID").trim(), true);
				for (int i = 1; i <= columns; i++) {
					wpstmt.setString(i, rs.getString(i));
					dataUpdatePs.setString(i, rs.getString(i));
				}
				try {
					if(dataGuidMap.containsKey(rs.getString("StationUserGUID").trim())){
						//存在即更新
						dataUpdatePs.setString(columns+1,rs.getString("StationUserGUID"));
						dataUpdatePs.executeUpdate();
					}else{
						//不存在即新增
						wpstmt.executeUpdate();
					}
					count++;
				} catch (Exception e) {
					e.printStackTrace();
					log.error(e);
				}
			}while (rs.next());
			
			String delSql = "delete from myStationUser where StationUserGUID =? ";
			PreparedStatement delPs = wcon.prepareStatement(delSql);
			log.info("delete sql:" + delSql);
			/**删除记录**/
			for(String key : dataGuidMap.keySet()){
				if(!myErpGuidMap.containsKey(key)){
					delPs.setString(1, key);
					delPs.executeUpdate();
				}
			}
			// 关闭
			ADataSource.close(rs);
			ADataSource.close(datars);
			ADataSource.close(pstmt);
			ADataSource.close(wpstmt);
			ADataSource.close(dataUpdatePs);
			ADataSource.close(dataps);
			ADataSource.close(delPs);

			log.info(count + " myStationUser save or update ");
			log.info("=============sync myStationUser end===========");

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ADataSource.close(con);
			ADataSource.close(wcon);
		}
	}// ~sync
}
